package com.leyou.item.service;

import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecificationService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;


    /**
     * 规格参数页,查询规格参数模版 组
     *
     * @param cid
     * @return
     */
    public List<SpecGroup> querySpecGroups(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        List<SpecGroup> groups = specGroupMapper.select(specGroup);
        groups.forEach(group->{
            group.setSpecParams(querySpecParams(group.getId(), cid, null, null));
        });

        return groups;
    }

    /**
     * 规格参数页,查询规格参数模版 组内规格
     *
     * @param gid
     * @param cid
     * @param generic
     * @param searching
     * @return
     */
    public List<SpecParam> querySpecParams(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setGeneric(generic);
        specParam.setSearching(searching);
        return this.specParamMapper.select(specParam);
    }


    /**
     * 规格参数  新增分组
     *
     * @param specGroup
     * @return
     */
    public boolean addGroup(SpecGroup specGroup) {
        if (StringUtils.isNotBlank(specGroup.getName())) {
            this.specGroupMapper.insertSelective(specGroup);
            return true;
        }
        return false;
    }


    /**
     * 规格参数  修改分组信息
     *
     * @param specGroup
     */
    public void editGroup(SpecGroup specGroup) {
        this.specGroupMapper.updateByPrimaryKey(specGroup);
    }


    /**
     * 规格参数  删除分组信息
     *
     * @param id
     */
    public void deleteGroup(Long id) {
        this.specGroupMapper.deleteByPrimaryKey(id);
    }


    /**
     * 规格参数页 分组内的参数  新增参数
     *
     * @param specParam
     * @return
     */
    public void addArguments(SpecParam specParam) {
        this.specParamMapper.insertSelective(specParam);
    }


    /**
     * 规格参数页 分组内的参数  修改参数
     *
     * @param specParam
     * @return
     */
    public void editArguments(SpecParam specParam) {
        this.specParamMapper.updateByPrimaryKey(specParam);
    }


    /**
     * 规格参数页 分组内的参数  删除参数
     *
     * @param id pid
     */
    public void deleteArguments(Long id) {
        this.specParamMapper.deleteByPrimaryKey(id);
    }
}
